import time

import page
from base.base import Base


class PageBackUserAddress(Base):
    # 点击用户地址链接
    def page_click_user_address_link(self):
        self.base_click_element(page.back_user_address)

    # 切换到用户地址iframe
    def page_switch_user_address_iframe(self):
        self.base_switch_to_iframe(page.back_user_address_iframe)

    def page_switch_parent_frame(self):
        self.base_switch_to_parent_frame()

    # 点击添加按钮
    def page_click_add_btn(self):
        self.base_click_element(page.back_add_btn)

    # 输入用户id
    def page_input_user_id(self, user_id):
        self.base_in_element_input(page.back_input_user_id, user_id)

    # 输入姓名
    def page_input_name(self, name):
        self.base_in_element_input(page.back_input_name, name)

    # 输入别名
    def page_input_alias(self, alias):
        self.base_in_element_input(page.back_input_alias, alias)

    # 输入手机号码
    def page_input_phone(self, tel):
        self.base_in_element_input(page.back_input_tel, tel)

    # 点击省市区下拉框
    def page_click_area_dropdown(self):
        self.base_click_element(page.back_click_area_dropdown)

    # 点击所在省
    def page_click_province(self):
        self.base_click_element(page.back_click_province)

    # 点击所在市
    def page_click_city(self):
        self.base_click_element(page.back_click_city)

    # 点击所在区
    def page_click_district(self):
        self.base_click_element(page.back_click_district)

    # 输入详细地址
    def page_input_detailed_address(self, address):
        self.base_in_element_input(page.back_input_detailed_address, address)

    # 点击是否默认
    def page_click_is_default(self):
        self.base_click_element(page.back_click_is_default)

    def page_scroll_to_status(self):
        self.base_scroll_to_element(page.back_input_id_card_number)

    def page_scroll_to_id_card_image(self):
        self.base_scroll_to_element(page.back_input_id_card_image)
    # 输入身份证姓名
    def page_input_id_card_name(self, id_name):
        self.base_in_element_input(page.back_input_id_card_name, id_name)

    # 输入身份证号码
    def page_input_id_card_number(self, id_number):
        self.base_in_element_input(page.back_input_id_card_number, id_number)

    # 提交添加按钮
    def page_click_address_submit_add(self):
        self.base_click_element(page.back_address_submit)

    # 点击编辑按钮
    def page_click_update_btn(self):
        self.base_click_element(page.get_address_id()['back_update_address'])

    def page_click_update_submit(self):
        self.base_click_element(page.back_address_update_submit)

    # 选择地址
    def page_select_address(self):
        self.base_click_element(page.get_address_id()['back_address_check'])

    # 点击删除被选择的地址按钮
    def page_click_delete_btn(self):
        self.page_select_address()
        self.base_click_element(page.back_delete_checked)
        time.sleep(1)
        self.base_click_element(page.product_confirm_btn)
        time.sleep(1)

    # 清空输入框
    def page_clear_input(self):
        self.base_clear_input(page.back_input_alias)

    # 获得截屏
    def page_get_screenshot(self):
        self.base_get_img()

    # 切换到用户地址页面
    def page_switch_to_user_address(self):
        self.page_switch_parent_frame()
        time.sleep(1)
        self.page_click_user_address_link()
        time.sleep(1)
        self.page_switch_user_address_iframe()
        time.sleep(1)
        self.page_click_add_btn()
        time.sleep(1)

    # 输入地址信息
    def page_input_address_info(self, user_id, name, tel, id_name, id_number,
                                alias=None, address=None, is_default=False):
        self.page_input_user_id(user_id)
        self.page_input_name(name)
        if alias:
            self.page_input_alias(alias)
        self.page_input_phone(tel)
        self.page_click_area_dropdown()
        self.page_click_province()
        self.page_click_city()
        self.page_click_district()
        if address:
            self.page_input_detailed_address(address)
        if is_default:
            self.page_click_is_default()
        self.page_scroll_to_status()
        self.page_input_id_card_name(id_name)
        self.page_input_id_card_number(id_number)
        self.page_click_address_submit_add()
        time.sleep(4)

    # 编辑地址信息
    def page_update_address_alias(self, alias):
        self.page_click_update_btn()
        self.page_clear_input()
        self.page_input_alias(alias)
        self.page_scroll_to_id_card_image()
        self.page_click_update_submit()
        time.sleep(2)
